# This runs the experiments as jobs on Dumbledore. The output of this
# program is CSV values in the following format:
#
#   num_procs, num_threads, a_rows, a_cols, b_cols, base_case_size, runtime

require 'fileutils'
require 'csv'

EXPERIMENTS_DIR = "experiments"

N = 70

MAX_NODES = 6
MAX_THREAD_POW = 3

a_rows = N
a_cols = N
b_cols = N
base_case_size = 100

FileUtils.mkdir_p(EXPERIMENTS_DIR)

def parse_execution_time(csv)
  # Knows the format returned by main, and knows that the runtime is
  # in a specific column.
  return CSV.parse_line(csv)[6].to_f
end




# Run strong experiments.
nodes = 1
  for thread_pow in 0..MAX_THREAD_POW
    threads = 0x01 << thread_pow
    output = File.join(EXPERIMENTS_DIR, "strong_#{nodes}_#{threads}.txt")
    File.unlink(output) if File.exist?(output)
    # puts "#{output}"
    rval = %x{qsub mpi_test.sh -v nodes=#{nodes},threads=#{threads},a_rows=#{a_rows},a_cols=#{a_cols},b_cols=#{b_cols},base_case_size=#{base_case_size} -o #{output} }
    # puts rval
    while !File.exist?(output)
      sleep(0.1)
    end
    text = File.open(output, "r")  { |f| f.read }
    csv = text.match(/result:(.*)$/)[1]
    puts "strong,#{csv.strip}"
 end

for nodes in 2..MAX_NODES
  threads = 0x01 << MAX_THREAD_POW
    output = File.join(EXPERIMENTS_DIR, "strong_#{nodes}_#{threads}.txt")
    File.unlink(output) if File.exist?(output)
    # puts "#{output}"
    rval = %x{qsub mpi_test.sh -v nodes=#{nodes},threads=#{threads},a_rows=#{a_rows},a_cols=#{a_cols},b_cols=#{b_cols},base_case_size=#{base_case_size} -o #{output} }
    # puts rval
    while !File.exist?(output)
      sleep(0.1)
    end
    text = File.open(output, "r")  { |f| f.read }
    csv = text.match(/result:(.*)$/)[1]
    puts "strong,#{csv.strip}"
end


# Run weak experiments.
for thread_pow in 0..MAX_THREAD_POW
	nodes = 1    
threads = 0x01 << thread_pow

    cores = nodes * threads
    this_n = (N**3)/16.0
    n = (cores * this_n)**(1.0/3.0)

    a_rows = n.round
    a_cols = n.round
    b_cols = n.round
#    base_case_size = 2

    output = File.join(EXPERIMENTS_DIR, "weak_#{nodes}_#{threads}.txt")
    File.unlink(output) if File.exist?(output)
    rval = %x{qsub mpi_test.sh -v nodes=#{nodes},threads=#{threads},a_rows=#{a_rows},a_cols=#{a_cols},b_cols=#{b_cols},base_case_size=#{base_case_size} -o #{output} }
    # puts rval
    while !File.exist?(output)
      sleep(0.1)
    end
    text = File.open(output, "r")  { |f| f.read }
    csv = text.match(/result:(.*)$/)[1]
    execution_time = parse_execution_time(csv)
    puts "weak,#{nodes},#{threads},#{a_rows},#{a_cols},#{b_cols},#{base_case_size},#{execution_time}"
  end


for nodes in 2..MAX_NODES

    threads = 0x01 << MAX_THREAD_POW

    cores = nodes * threads
    this_n = (N**3)/16.0
    n = (cores * this_n)**(1.0/3.0)

    a_rows = n.round
    a_cols = n.round
    b_cols = n.round
#    base_case_size = 2

    output = File.join(EXPERIMENTS_DIR, "weak_#{nodes}_#{threads}.txt")
    File.unlink(output) if File.exist?(output)
    rval = %x{qsub mpi_test.sh -v nodes=#{nodes},threads=#{threads},a_rows=#{a_rows},a_cols=#{a_cols},b_cols=#{b_cols},base_case_size=#{base_case_size} -o #{output} }
    # puts rval
    while !File.exist?(output)
      sleep(0.1)
    end
    text = File.open(output, "r")  { |f| f.read }
    csv = text.match(/result:(.*)$/)[1]
    execution_time = parse_execution_time(csv)
    puts "weak,#{nodes},#{threads},#{a_rows},#{a_cols},#{b_cols},#{base_case_size},#{execution_time}"
  

end

